#include "configurationdlgimpl.h"

#include <QtGui>

//
ConfigurationDlgImpl::ConfigurationDlgImpl( QWidget * parent, Qt::WFlags f) 
	: QDialog(parent, f)
{
	setupUi(this);
	
	// Fill size combo.
	fillComboSize();
	
	m_currentDefaultFontColor = QColor(Qt::black);
	m_currentEditorColor = QColor(Qt::white);
	m_currentTreeColor = QColor(Qt::white);
	
	drawColor(tbDefaultFontColor, m_currentDefaultFontColor);
	drawColor(tbEditorColor, m_currentEditorColor);
	drawColor(tbTreeColor, m_currentTreeColor);
}

void ConfigurationDlgImpl::setConfigurationManager(ConfigurationManager *confMgr)
{
	m_confManager = confMgr;
	
	m_currentDefaultFontColor = m_confManager->defaultFontColor;
	m_currentEditorColor = m_confManager->editorColor;
	m_currentTreeColor = m_confManager->treeColor;
	
	drawColor(tbDefaultFontColor, m_currentDefaultFontColor);
	drawColor(tbEditorColor, m_currentEditorColor);
	drawColor(tbTreeColor, m_currentTreeColor);
	
	int fontIndex = cbDefaultFont->findText(m_confManager->defaultFont);
	if (fontIndex != -1)
		cbDefaultFont->setCurrentIndex(fontIndex);
	
	int sizeIndex = cbDefaultFontSize->findText(m_confManager->defaultFontSize);
	if (sizeIndex != -1)
		cbDefaultFontSize->setCurrentIndex(sizeIndex);
	
	ckUseTrayIcon->setChecked(m_confManager->useTrayIcon);
	ckCloseToTray->setChecked(m_confManager->closeToTray);
	ckShowMessageAtClosing->setChecked(m_confManager->showMessageOnCloseToTray);
	ckCloseToTray->setEnabled(ckUseTrayIcon->isChecked());
	ckShowMessageAtClosing->setEnabled(ckUseTrayIcon->isChecked() && ckCloseToTray->isChecked());
	
	ckOpenLastFile->setChecked(m_confManager->openLastFile);
	
	leDateFormat->setText(m_confManager->dateFormat);
}

void ConfigurationDlgImpl::updateConfigurationManager()
{
	m_confManager->defaultFont = cbDefaultFont->currentText();
	m_confManager->defaultFontSize = cbDefaultFontSize->currentText();
	m_confManager->defaultFontColor = m_currentDefaultFontColor;
	m_confManager->editorColor = m_currentEditorColor;
	m_confManager->treeColor = m_currentTreeColor;
	
	m_confManager->useTrayIcon = ckUseTrayIcon->isChecked();
	m_confManager->closeToTray = ckCloseToTray->isChecked();
	m_confManager->showMessageOnCloseToTray = ckShowMessageAtClosing->isChecked();
	
	m_confManager->openLastFile = ckOpenLastFile->isChecked();
	
	m_confManager->dateFormat = leDateFormat->text();
}

void ConfigurationDlgImpl::drawColor(QToolButton *button, QColor color)
{
    QPixmap pix(16, 16);
    pix.fill(color);
	button->setIcon(pix);
}
//

void ConfigurationDlgImpl::fillComboSize()
{
    // Fill size combo.
    QFontDatabase db;
    foreach(int size, db.standardSizes())
    cbDefaultFontSize->addItem(QString::number(size));
    cbDefaultFontSize->setCurrentIndex(cbDefaultFontSize->findText(QString::number(QApplication::font().pointSize())));
}

void ConfigurationDlgImpl::on_tbDefaultFontColor_clicked()
{
	QColor col = QColorDialog::getColor(m_currentDefaultFontColor, this);
    if (!col.isValid())
        return;

    drawColor(tbDefaultFontColor, col);
    m_currentDefaultFontColor = col;
}

void ConfigurationDlgImpl::on_tbEditorColor_clicked()
{
	QColor col = QColorDialog::getColor(m_currentEditorColor, this);
    if (!col.isValid())
        return;

    drawColor(tbEditorColor, col);
    m_currentEditorColor = col;
}

void ConfigurationDlgImpl::on_tbTreeColor_clicked()
{
	QColor col = QColorDialog::getColor(m_currentTreeColor, this);
    if (!col.isValid())
        return;

    drawColor(tbTreeColor, col);
    m_currentTreeColor = col;
}

void ConfigurationDlgImpl::on_okButton_clicked()
{
	updateConfigurationManager();
}

void ConfigurationDlgImpl::on_ckUseTrayIcon_clicked()
{
	ckCloseToTray->setEnabled(ckUseTrayIcon->isChecked());
	ckShowMessageAtClosing->setEnabled(ckUseTrayIcon->isChecked() && ckCloseToTray->isChecked());
}

void ConfigurationDlgImpl::on_ckCloseToTray_clicked()
{
	ckShowMessageAtClosing->setEnabled(ckUseTrayIcon->isChecked() && ckCloseToTray->isChecked());
}
